<?php
class Tourismprofitrate extends CS_Controller
{
    public function _init()
    {
        $this->load->helper(array('common'));
        $this->load->library('pagination');
        $this->load->model('tourism_profit_rate_model','tourism_profit_rate');
        $this->load->model('user_model','user');
        $this->load->model('user_account_model','user_account');
    }
    
     /**
      * 分润列表
     */
    public function grid($pg=1) 
    {
        $getData = $this->input->get();
        $getData['uid'] = $this->uid;
        $page_num = 15;
        $num = ($pg - 1) * $page_num;
        $config['first_url'] = base_url('tourismprofitrate/grid') . $this->pageGetParam($this->input->get());
        $config['suffix'] = $this->pageGetParam($this->input->get());
        $config['base_url'] = base_url('tourismprofitrate/grid');
        $config['total_rows'] = $this->tourism_profit_rate->total($getData);
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_link'] = $this->pagination->create_links();
        $data['page_list'] = $this->tourism_profit_rate->page_list($page_num, $num, $getData);
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['pg_num'] = $page_num;
        $this->load->view('tourismprofitrate/grid',$data);
    }
     
     /**
     * 删除
     * @param unknown $rate_id
     */
    public function logicDelete($rate_id) 
    {
        $result = $this->tourism_profit_rate->logicDelete($rate_id);
        if ($result) {
            $this->success('tourismprofitrate/grid', '', '删除成功');
        }
        $this->error('tourismprofitrate/grid', '', '删除失败');
    }
    
     /**
     *  添加
     */
    public function add() 
    {
        $data['beizhu'] = $this->getPoundage();
        $this->load->view('tourismprofitrate/add',$data);
    }
    
     /**
      * 添加分润列表
     */
    public function addPost()
    {
        $_d = $this->input->post();
        $_d['beizhu'] = $this->getPoundage();
        $d = $_d;
        unset($d['name']);
        unset($d['purchaser_id']);
        unset($d['manager_l1_id']);
        unset($d['manager_l2_id']);
        unset($d['growth_id']);
        unset($d['seller_id']);
        if (array_sum($d) != 100) {
            $this->ajaxJson(['status' => false, 'msg' => '分成比率相加不等于100']);
        }
        $_d['uid'] = $this->uid;
        $result = $this->tourism_profit_rate->insert($_d);
        if ($result) {
            $this->ajaxJson(['status' => true, 'msg' => site_url('tourismprofitrate/grid')]);
        }
        $this->ajaxJson(['status' => false, 'msg' => '添加失败']);
    }
    
     /**
     * 编辑
     * @param unknown $rate_id
     */
    public function edit($rate_id) 
    {
        $data['beizhu'] = $this->getPoundage();
        if (empty($rate_id)) {
            $this->error('tourismprofitrate/grid', '', 'rate_id没有');
        }
        $result = $this->tourism_profit_rate->findOne(array('rate_id'=>$rate_id));
        if ($result->num_rows()<=0) {
            $this->error('tourismprofitrate/grid', '', '没有结果');
        }
        $profirate = $result->row_array();
        $nameArr = array();
        foreach ($profirate as $key => $value) {
            if (is_numeric($value) && !empty($value) && $value > 10000) {
                $user= $this->user->findByUid($value)->row();
                $nameArr[$key] = $user->user_name;
            }
        }
        $data['nameArr'] = $nameArr;
        $data['user'] = $result->row(0);
        $this->load->view('tourismprofitrate/edit',$data);
    }
    
     /**
     * 编辑
     */
    public function editPost()
    {
        $_d = $this->input->post();
        $_d['beizhu'] = $this->getPoundage();
        $rate_id = $_d['rate_id'];
        unset($_d['rate_id']);
        $d = $_d;
        unset($d['rate_id']);
        unset($d['name']);
        unset($d['prosecond_id']);
        unset($d['purchaser_id']);
        unset($d['manager_l1_id']);
        unset($d['manager_l2_id']);
        unset($d['growth_id']);
        unset($d['seller_id']);
        if (array_sum($d) != 100) {
            $this->ajaxJson(['status' => false, 'msg' => '分成比率相加不等于100']);
        }
        $_d['uid'] = $this->uid;
        $result = $this->tourism_profit_rate->update($_d, ['rate_id' => $rate_id]);
        if ($result) {
            $this->ajaxJson(['status' => true, 'msg' => site_url('tourismprofitrate/grid')]);
        }
        $this->ajaxJson(['status' => false, 'msg' => '更新失败']);
    }
    
    public function ajaxQueryId()
    {
        $namephone = $this->input->post('namephone');
        if (valid_mobile($namephone)){
            $res = $this->user->findOne(['mobile_phone' => $namephone]);
            if ($res->num_rows() != 1){
                $this->ajaxJson(['status' => false, 'msg' => '用户不存在或存在多条']);
            }
        }else {
            $res = $this->user->findOne(['user_name' => $namephone]);
            if ($res->num_rows() != 1){
                $this->ajaxJson(['status' => false, 'msg' => '用户不存在或存在多条']);
            }
        }
        $id = $res->row()->uid;
        $this->ajaxJson(true,site_url('tourismprofitrate/grid'),['id' => $id]);
    }
       
    private function getPoundage()
    {
        $result = $this->user_account->findByUid($this->uid);
        return $result->row()->poundage_rate * 100;
    }
}